{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9baae365-6875-465b-b0b0-88abfdfd4501",
   "metadata": {},
   "source": [
    "### canny 边缘检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ebf2b17f-bd8d-4a7b-b0fc-29c9377ebd34",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "from cv2_common import im_mul_show,im_show\n",
    "# img = cv2.imread('./image/test_02.jpg')\n",
    "img_origin = cv2.imread('./image/test.jpg')\n",
    "img = cv2.resize(img_origin,None,fx=0.4,fy=0.4)\n",
    "img2 = cv2.resize(img_origin,None,fx=0.4,fy=0.4,interpolation=cv2.INTER_BITS)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "12352013-99ab-4df5-851b-14d9f439d08e",
   "metadata": {},
   "outputs": [],
   "source": [
    "im_mul_show(oring=img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "388ce68a-cd6f-4426-bc2f-1902d3c96bce",
   "metadata": {},
   "outputs": [],
   "source": [
    "#阈值对检测结果的影响,阈值越小会获得更多的细节，提取图像更多边缘。\n",
    "canny = cv2.Canny(img,0,50)\n",
    "canny2 = cv2.Canny(img,100,200)\n",
    "im_mul_show(origin=img,canny=canny,canny2 = canny2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "36c3788a-897b-40cb-8ea1-abf0c066daef",
   "metadata": {},
   "outputs": [],
   "source": [
    "#sobel算子对检测结果的影响,增大算子，会获得更多的细节，提取图像更多边缘\n",
    "canny = cv2.Canny(img,100,200,apertureSize=3)\n",
    "canny2 = cv2.Canny(img,100,200,apertureSize=5)\n",
    "im_mul_show(origin=img,canny=canny,canny2 = canny2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "438036ef-cf56-498d-9ff3-5e8b921fd161",
   "metadata": {},
   "outputs": [],
   "source": [
    "#范数对检测结 L2gradient=True时，检测出的边缘减少了\n",
    "canny = cv2.Canny(img,100,200,L2gradient=False)\n",
    "canny2 = cv2.Canny(img,100,200,L2gradient=True)\n",
    "im_mul_show(origin=img,canny=canny,canny2 = canny2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28fd746d-19a1-4d35-84c4-b616bf113642",
   "metadata": {},
   "source": [
    "### 二值化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d128e028-0013-464e-83fc-ef58dfdabc43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100.0\n"
     ]
    }
   ],
   "source": [
    "# 全局二值化\n",
    "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
    "# cv2.THRESH_BINARY：大于阈值时置 255，否则置 0\n",
    "# cv2.THRESH_BINARY_INV：大于阈值时置 0，否则置 255\n",
    "# cv2.THRESH_TRUNC：大于阈值时置为阈值 thresh，否则不变（保持原色）\n",
    "thresh, threshold= cv2.threshold(gray,100,255,cv2.THRESH_BINARY) # 返回阈值 和二值图 \n",
    "print(thresh)\n",
    "im_mul_show(origin=img,gray=gray,threshold=threshold)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bd3b737-1900-443e-b78b-35d32a0e4d7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自适应二值化\n",
    "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
    "# cv2.ADAPTIVE_THRESH_MEAN_C：相邻区域平均值\n",
    "# cv2.ADAPTIVE_THRESH_GAUSSIAN_C：相邻区域加权和，权重为高斯窗口\n",
    "threshold= cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,6)\n",
    "im_mul_show(origin=img,gray=gray,threshold=threshold)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee2da882-5635-429e-aa77-d24199eb71be",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
